package com.xujiakai.car.maintain.domain.entity;

import com.alibaba.fastjson.JSON;
import com.xujiakai.car.maintain.application.service.impl.UserValidateService;
import com.xujiakai.car.maintain.domain.dto.AccountDTO;
import com.xujiakai.car.maintain.domain.dto.PermissionDTO;
import com.xujiakai.car.maintain.infrastructure.mapper.DaoFactory;
import com.xujiakai.car.maintain.userinterface.views.AccordingElement;
import com.xujiakai.car.maintain.userinterface.views.AccordingItem;
import com.xujiakai.car.maintain.userinterface.views.AccrodingPanel;
import com.xujiakai.car.maintain.userinterface.views.LoginBean;
import com.xujiakai.car.maintain.userinterface.vo.AccountVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class Account{
    private Integer id;

    private String accountnumber;

    private String passwords;

    private String accountflag;

    private Integer userinfoid;

    @Resource
    private DaoFactory daoFactory;

    private AccountDTO accountDTO;
    private AccountVO accountVO;

    public Integer getEntityId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAccountnumber() {
        return accountnumber;
    }

    public void setAccountnumber(String accountnumber) {
        this.accountnumber = accountnumber == null ? null : accountnumber.trim();
    }

    public String getPasswords() {
        return passwords;
    }

    public void setPasswords(String passwords) {
        this.passwords = passwords == null ? null : passwords.trim();
    }

    public String getAccountflag() {
        return accountflag;
    }

    public void setAccountflag(String accountflag) {
        this.accountflag = accountflag == null ? null : accountflag.trim();
    }

    public Integer getUserinfoid() {
        return userinfoid;
    }

    public void setUserinfoid(Integer userinfoid) {
        this.userinfoid = userinfoid;
    }


    public AccrodingPanel queryUserPrivleges(AccountVO accountVO) {
        AccountDTO account = voToDto(accountVO);
        // 先获取到用户所有的权限
        if (account == null) {
            return null;
        }
        AccrodingPanel accordingPanel = null;
        try {
            List<PermissionDTO> permissions = daoFactory.getPermissionMapper()
                    .getAllUserPermission(account.getAccountnumber());
            if (permissions != null && permissions.size() > 0) {
                accordingPanel = new AccrodingPanel();
                Set<String> history = new HashSet<>();
                String currentCategory = "";
                // 开始解析数据
                accordingPanel = new AccrodingPanel();
                // 里面的每个大项
                List<AccordingElement> elements = new ArrayList<>();
                // 1.先解析出所有的大类
                // 2.依据大类再抽取出所有的子类
                for (PermissionDTO p : permissions) {
                    currentCategory = p.getAuthcategory();
                    // 如果是新类
                    if(!history.contains(currentCategory)){
                        // 实例化一个项目
                        AccordingElement element = new AccordingElement();
                        element.setTitle(currentCategory);
                        // 开始解析这个大项目下的所有子项目
                        List<AccordingItem> items = new ArrayList<>();
                        for(PermissionDTO _p:permissions){
                            // 如果符合大类
                            if(currentCategory.equals(_p.getAuthcategory())){
                                AccordingItem item = new AccordingItem();
                                item.setId(_p.getId());
                                item.setText(_p.getPername());
                                item.setIconCls(_p.getPerurl().split("#")[1]);
                                Map<String, Object> attributes = new HashMap<String, Object>();
                                attributes.put("url", _p.getPerurl().split("#")[0]);
                                item.setAttributes(attributes);
                                // 添加到item中
                                items.add(item);
                            }
                            element.setData(items);
                            element.setDataJSON(JSON.toJSONString(items).replace("\"","\'"));
                        }
                        // 把这个项目添加到elements中
                        elements.add(element);
                        // 添加到历史记录中
                        history.add(currentCategory);
                    }
                }
                // 最后把所有的面板项项加到面板中
                accordingPanel.setElements(elements);
            }
        } catch (Exception e) {
            e.printStackTrace();
            UserValidateService.logger.error(e.toString());
        }
        UserValidateService.logger.info("返回到前台的所有的panel格式如下：-->"+JSON.toJSONString(accordingPanel));
        return accordingPanel;
    }

    public LoginBean userislawable(AccountVO account) {
        if (account == null) {
            return null;
        }
        LoginBean loginBean = null;
        try {
            loginBean = daoFactory.getAccontMapper().selectcount(voToDto(account));
        } catch (Exception e) {
            UserValidateService.logger.error(e.toString());
        }
        return loginBean;
    }

    public AccountDTO voToDto(AccountVO account) {
        AccountDTO accountDTO = new AccountDTO();
        accountDTO.setAccountnumber(account.getAccountnumber());
        accountDTO.setPasswords(account.getPasswords());
        accountDTO.setAccountflag(account.getAccountflag());
        accountDTO.setUserinfoid(account.getUserinfoid());
        return accountDTO;
    }

}